﻿@model AddressModel
@if (Model.CountryEnabled && Model.StateProvinceEnabled)
{
    var ipAddress = EngineContext.Current.Resolve<IWebHelper>().GetCurrentIpAddress();

    //默认中国
    Model.CountryId = 23;
    <script type="text/javascript">
            var  CountryId=@Model.CountryId;  //默认中国
            var provinceId = @(Model.StateProvinceId.HasValue ? Model.StateProvinceId.Value : 0);
            var cityId = @(Model.CityId.HasValue ? Model.CityId.Value : 0);
            var districtId = @(Model.DistrictId.HasValue ? Model.DistrictId.Value : 0);
            var currentIP = '@ipAddress', province = '', city = '', district = '';
            $(function () {
                var ddlStates = $("#@Html.FieldIdFor(model => model.StateProvinceId)");
                var drpCity = $("#@Html.FieldIdFor(model => model.CityId)");
                var drpDistrict = $("#@Html.FieldIdFor(model => model.DistrictId)");
                // ==== 填充 省 列表 == start =====
                $.ajax({
                    type: "GET",
                    url: "@(Url.Action("GetStatesByCountryId", "Country"))",
                    data: { "countryId": CountryId, "addEmptyStateIfRequired": "true" },
                }).done(function (data) {
                    if ($.isArray(data)) {
                        ddlStates.html('');
                        $.each(data, function (id, option) {
                            ddlStates.append($('<option></option>').val(option.id).html(option.name));
                        });
                        if (data) { //定位到已选择的省ID
                            var selectedIndex = getSelectedIndex(data, provinceId);
                            if (selectedIndex > 0) {
                                ddlStates.val(provinceId);
                            }

                            ddlStates.trigger("change");
                        }

                    }
                });

                $("#@Html.FieldIdFor(model => model.StateProvinceId)").change(function (evt, param) {
                    var selectedItem = $(this).val();
                    $.ajax({
                        cache: false,
                        type: "GET",
                        url: "@(Url.Action("GetCitysByProvinceId", "Country"))",
                        data: { "stateId": selectedItem, "addEmptyStateIfRequired": "true" },
                        success: function (data) {
                            drpCity.html('');
                            $.each(data, function (id, option) {
                                drpCity.append($('<option></option>').val(option.id).html(option.name));
                            });
                            if (data) {
                                //定位到已选择的市ID
                                var selectedIndex = getSelectedIndex(data, cityId);
                                if (selectedIndex > 0) {
                                    drpCity.val(cityId);
                                }
                                drpCity.trigger('change');
                            }


                        },
                        error: function (xhr, ajaxOptions, thrownError) {
                            alert('Failed to retrieve states.');
                        }
                    });
                    $("#@Html.FieldIdFor(model => model.CityId)").change(function (evt, param) {
                        var selectedItem = $(this).val();
                        $.ajax({
                            cache: false,
                            type: "GET",
                            url: "@(Url.Action("GetDistrictsByCityId", "Country"))",
                            data: { "cityId": selectedItem, "addEmptyStateIfRequired": "true" },
                            success: function (data) {
                                drpDistrict.html('');
                                $.each(data, function (id, option) {
                                    drpDistrict.append($('<option></option>').val(option.id).html(option.name));
                                });
                                if (data) {
                                    //定位到已选择的区ID
                                    var selectedIndex = getSelectedIndex(data, districtId);
                                    if (selectedIndex > 0) {
                                        drpDistrict.val(districtId);

                                    }
                                    drpDistrict.trigger("change");
                                }
                            },
                            error: function (xhr, ajaxOptions, thrownError) {
                                alert('Failed to retrieve states.');
                            }
                        });
                    });
                });
            });
            function getSelectedIndex(data, id) {
                var selectedIndex = -1;
                var selected = false;
                if (id) {
                    for (var j = 0; j < data.length; j++) {
                        if (data[j].id == id.toString()) {
                            selected = true;
                            selectedIndex = j;
                            break;
                        }
                    }
                }
                return selectedIndex;
            }


    </script>
}
@Html.HiddenFor(model => model.Id)
@Html.HiddenFor(model => model.FirstNameEnabled)
@Html.HiddenFor(model => model.FirstNameRequired)
@Html.HiddenFor(model => model.LastNameEnabled)
@Html.HiddenFor(model => model.LastNameRequired)
@Html.HiddenFor(model => model.EmailEnabled)
@Html.HiddenFor(model => model.EmailRequired)
@Html.HiddenFor(model => model.ValidateEmailAddress)
@Html.HiddenFor(model => model.CompanyEnabled)
@Html.HiddenFor(model => model.CompanyRequired)
@Html.HiddenFor(model => model.CountryEnabled)
@Html.HiddenFor(model => model.StateProvinceEnabled)
@Html.HiddenFor(model => model.CityEnabled)
@Html.HiddenFor(model => model.CityRequired)
@Html.HiddenFor(model => model.StreetAddressEnabled)
@Html.HiddenFor(model => model.StreetAddressRequired)
@Html.HiddenFor(model => model.StreetAddress2Enabled)
@Html.HiddenFor(model => model.StreetAddress2Required)
@Html.HiddenFor(model => model.ZipPostalCodeEnabled)
@Html.HiddenFor(model => model.ZipPostalCodeRequired)
@Html.HiddenFor(model => model.PhoneEnabled)
@Html.HiddenFor(model => model.PhoneRequired)
@Html.HiddenFor(model => model.FaxEnabled)
@Html.HiddenFor(model => model.FaxRequired)
<div class="form-body">
    @if (Model.FirstNameEnabled)
    {
        <div class="form-group">
            <label class="control-label col-md-3">   @Html.LangLabelFor(model => model.FirstName)</label>
            <div class="col-md-3">
                @Html.TextBoxFor(model => Model.FirstName, new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.FirstName)
            </div>
        </div>
    }
    @if (Model.LastNameEnabled)
    {
        <div class="form-group">
            <label class="control-label col-md-3">  @Html.LangLabelFor(model => model.LastName)</label>
            <div class="col-md-3">
                @Html.TextBoxFor(model => Model.LastName, new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.LastName)
            </div>
        </div>
    }
    @if (Model.EmailEnabled)
    {
        <div class="form-group">
            <label class="control-label col-md-3">  @Html.LangLabelFor(model => model.Email)</label>
            <div class="col-md-3">
                @Html.TextBoxFor(model => Model.Email, new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.Email)
            </div>
        </div>
    }
    @if (Model.ValidateEmailAddress && Model.EmailEnabled)
    {
        <div class="form-group">
            <label class="control-label col-md-3">  @Html.LangLabelFor(model => model.EmailMatch)</label>
            <div class="col-md-3">
                @Html.TextBoxFor(model => Model.EmailMatch, new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.EmailMatch)
            </div>
        </div>
    }
    @if (Model.CompanyEnabled)
    {
        <div class="form-group">
            <label class="control-label col-md-3">  @Html.LangLabelFor(model => model.Company)</label>
            <div class="col-md-3">
                @Html.TextBoxFor(model => Model.Company, new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.Company)
            </div>
        </div>
    }
    @if (Model.CountryEnabled)
    {
        <div class="form-group">
            <label class="control-label col-md-3">  @Html.LangLabelFor(model => model.CountryId)</label>
            <div class="col-md-3">
                @Html.DropDownListFor(model => model.CountryId, Model.AvailableCountries, T("Seller.Address.SelectCountry").Text, new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.CountryId)
            </div>
        </div>
    }
    @if (Model.CountryEnabled && Model.StateProvinceEnabled)
    {
        <div class="form-group">
            <label class="control-label col-md-3">  @Html.LangLabelFor(model => model.StateProvinceId)</label>
            <div class="col-md-3">
                @Html.DropDownListFor(model => model.StateProvinceId, Model.AvailableStates, new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.StateProvinceId)
            </div>
        </div>
        <div class="form-group">
            <label class="control-label col-md-3">  @Html.LangLabelFor(model => model.CityId)</label>
            <div class="col-md-3">
                @Html.DropDownListFor(model => model.CityId, Model.AvailableCitys, new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.CityId)
            </div>
        </div>
        <div class="form-group">
            <label class="control-label col-md-3">  @Html.LangLabelFor(model => model.DistrictId)</label>
            <div class="col-md-3">
                @Html.DropDownListFor(model => model.DistrictId, Model.AvailableDistricts, new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.DistrictId)
            </div>
        </div>
 
    }
    
    @if (Model.StreetAddressEnabled)
    {
        <div class="form-group">
            <label class="control-label col-md-3">  @Html.LangLabelFor(model => model.Address1)</label>
            <div class="col-md-3">
                @Html.TextBoxFor(model => Model.Address1, new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.Address1)
            </div>
        </div>


    }
    @if (Model.StreetAddress2Enabled)
    {
        <div class="form-group">
            <label class="control-label col-md-3">  @Html.LangLabelFor(model => model.Address2)</label>
            <div class="col-md-3">
                @Html.TextBoxFor(model => Model.Address2, new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.Address2)
            </div>
        </div>
    }
    @if (Model.ZipPostalCodeEnabled)
    {
        <div class="form-group">
            <label class="control-label col-md-3">  @Html.LangLabelFor(model => model.ZipPostalCode)</label>
            <div class="col-md-3">
                @Html.TextBoxFor(model => Model.ZipPostalCode, new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.ZipPostalCode)
            </div>
        </div>
    }
    @if (Model.PhoneEnabled)
    {
        <div class="form-group">
            <label class="control-label col-md-3">  @Html.LangLabelFor(model => model.PhoneNumber)</label>
            <div class="col-md-3">
                @Html.TextBoxFor(model => Model.PhoneNumber, new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.PhoneNumber)
            </div>
        </div>
    }
    @if (Model.FaxEnabled)
    {
        <div class="form-group">
            <label class="control-label col-md-3">  @Html.LangLabelFor(model => model.FaxNumber)</label>
            <div class="col-md-3">
                @Html.TextBoxFor(model => Model.FaxNumber, new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.FaxNumber)
            </div>
        </div>
    }
</div>

